OS = $(shell uname)
ifeq ($(OS),FreeBSD)
PTHREAD = -pthread
else
PTHREAD = -lpthread
endif

AR      = ar
ARFLAGS = crv
RANLIB  = :
SHARED = -shared

# Compiler. Default gcc
CC = gcc
CXX = g++

# Compiler switches. Default -g.
ifeq ($(RELEASE),1)
	CXXFLAGS = -g -DNDEBUG -Wall -O3 -I. -L.
else
	CXXFLAGS = -g -pg -DDEBUG -Wall -I. -L. 
endif

OBJDIR = ../obj
LIBDIR = ../lib
BINDIR = ../bin

LIBYPIN = $(LIBDIR)/libtest.a
LIBYPIN_SO = $(LIBDIR)/libtest.so

BINS = test 
ABSBINS = $(patsubst %,$(BINDIR)/%,$(BINS))

OBJS =  test_a.o

ABSOBJS = $(patsubst %,$(OBJDIR)/%,$(OBJS))

LDLIBPATH = -L/usr/local/lib 

#all : $(ABSBINS) 
all : $(LIBYPIN) $(LIBYPIN_SO) $(ABSBINS) 
$(LIBYPIN) : $(ABSOBJS)
	@ test -d $(LIBDIR) || mkdir -p $(LIBDIR)
	rm -f $(LIBYPIN)
	$(AR) $(ARFLAGS) $(LIBYPIN) $(ABSOBJS)
	$(RANLIB) $(LIBYPIN)

$(LIBYPIN_SO) : $(ABSOBJS)
	@ test -d $(LIBDIR) || mkdir -p $(LIBDIR)
	rm -f $(LIBYPIN_SO)
	$(CC) $(CXXFLAGS) $(SHARED) $(LDLIBPATH) -o $(LIBYPIN_SO) $(ABSOBJS) $(PTHREAD)

$(OBJDIR)/%.o : %.c
	@ test -d $(OBJDIR) || mkdir -p $(OBJDIR)
	$(CC) -o $@ -c $(CXXFLAGS) $<

$(OBJDIR)/%.o : %.cpp
	@ test -d $(OBJDIR) || mkdir -p $(OBJDIR)
	$(CC) -o $@ -c $(CXXFLAGS) $< 

$(ABSBINS): test.c $(ABSOBJS)
	@ test -d $(BINDIR) || mkdir -p $(BINDIR)
	$(CC) $(CXXFLAGS) -o $@ $^ $(LDLIBPATH) $(PTHREAD) 

clean:
	rm -f $(ABSOBJS)
	rm -f $(ABSBINS)
	rm -f $(LIBYPIN_SO)
	rm -f $(LIBYPIN)
